//
//  HttpTool.h
//  AFNetWorking 3.0 工具类
//
//  Created by peony on 15/7/21.
//  Copyright (c) 2015年 周生龙. All rights reserved.
//

#import "HttpTool.h"
#import "AFNetworking.h"

@interface HttpTool ()

@end
@implementation HttpTool
/**
 * Get请求
 */
+ (void)get:(NSString *)url params:(NSDictionary *)params success:(void (^)(id))success failure:(void (^)(NSError *))failure {
    // 1.获得请求管理者
    AFHTTPSessionManager * mgr = [AFHTTPSessionManager manager];
   
    // 2.发送GET请求
    [mgr GET:url parameters:params progress:^(NSProgress * _Nonnull downloadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        if (success) {
            success(responseObject);
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        if (failure) {
            failure(error);
            [SVProgressHUD showErrorWithStatus:@"连接服务器超时"];
        }
    }];

}
/**
 * Post请求
 */
+ (void)post:(NSString *)url params:(NSDictionary *)params success:(void (^)(id))success failure:(void (^)(NSError *))failure {
    // 1.获得请求管理者
    AFHTTPSessionManager * mgr = [AFHTTPSessionManager manager];
//    [mgr.requestSerializer setValue:@"ios_poneydocotr/2.0" forHTTPHeaderField:@"User-Agent"];
    // 2.发送POST请求
    [mgr POST:url parameters:params progress:^(NSProgress * _Nonnull uploadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {

        if (success) {
            success(responseObject);
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        if (failure) {
            failure(error);
            [SVProgressHUD showErrorWithStatus:@"连接服务器超时"];
        }
    }];

}
/**
 *  POST上传文件请求
 */
+ (void)post:(NSString *)url params:(NSDictionary *)params dataSource:(FormData *)dataSource success:(void (^)(id))success failure:(void (^)(NSError *))failure {
    
    // 1.获得请求管理者
    AFHTTPSessionManager * mgr = [AFHTTPSessionManager manager];
 
    // 2.请求内容
    [mgr POST:url parameters:params constructingBodyWithBlock:^(id<AFMultipartFormData>  _Nonnull formData) {
        [formData appendPartWithFileData:dataSource.data name:dataSource.paramsName fileName:dataSource.filename mimeType:dataSource.mimeType];
    } progress:^(NSProgress * _Nonnull uploadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        if (success) {
            success(responseObject);

        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        if (failure) {
            failure(error);
            [SVProgressHUD showErrorWithStatus:@"服务器异常"];
        }
    }];

}

@end
/**
 *  用来封装文件数据的模型
 */
@implementation FormData



@end








